package com.example.keli.ui.center;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.keli.R;
import com.example.keli.api.RetrofitManager;
import com.example.keli.base.BaseFragment;
import com.example.keli.base.app.C;
import com.kennyc.view.MultiStateView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * description $desc$
 * created by jerry on 2019/3/18.
 */
public class ActiveFragment extends BaseFragment {

    private static final String TAG = "ActiveFragment";
    @BindView(R.id.recycler_active)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SwipeRefreshLayout mRefreshlayout;
    @BindView(R.id.multiStateView)
    MultiStateView multiStateView;
    private ActiveAdapter activeAdapter;
    private List<ActivieList.DataBean> mActives;
//    "1待启动", "2报名中", "3进行中", "4已结束"
    private int mStatus = 3;
    private int page = 1;
//    private String mRegionId = "39";//城市id  默认济南

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSignSucess(String event){
        if (C.EVENT.SIGN_SUCCESS.equals(event) && mStatus==3){
            page = 1;
            mActives.clear();
            loadDatas();
        }
    }

    public static ActiveFragment newInstance(int status) {
        ActiveFragment aboutFragment = new ActiveFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("status", status);
        aboutFragment.setArguments(bundle);
        return aboutFragment;
    }

    @Override
    protected int layoutId() {
        return R.layout.fragment_active;
    }

    @Override
    protected void bindData() {
        EventBus.getDefault().register(this);
        Bundle bundle = getArguments();
        if (bundle != null){
            mStatus = bundle.getInt("status");
        }
        initRecycleView();
        loadDatas();
    }

    private void initRecycleView() {
        mActives = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        activeAdapter = new ActiveAdapter(mActives, getContext(), mStatus);
        recyclerView.setAdapter(activeAdapter);
        multiStateView.setViewState(MultiStateView.VIEW_STATE_LOADING);
    }

    @Override
    protected void bindListener() {
        mRefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mActives.clear();
                loadDatas();
            }
        });
        activeAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadDatas();
            }
        }, recyclerView);
        activeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ActivieList.DataBean dataBean = (ActivieList.DataBean) adapter.getItem(position);
                if (dataBean !=null){
                    ActiveDetailsActivity.start(getActivity(), dataBean.getId());
                }
            }
        });
    }

    private void loadDatas() {
        Map<String, String> map = new HashMap<>();
//        map.put("region_id", mRegionId);
        map.put("status", String.valueOf(mStatus));
        map.put("page", String.valueOf(page));
        RetrofitManager.ApiFunc(RetrofitManager.mApiService.getActivity(map), new SubscriberListener<BaseResponse<ActivieList>>() {
            @Override
            public void onSuccess(BaseResponse<ActivieList> activieListBaseResponse) {
                if (page == 1) {
                    mRefreshlayout.setRefreshing(false);
                    if(activieListBaseResponse.data.getData().size()==0){
                        multiStateView.setViewState(MultiStateView.VIEW_STATE_EMPTY);
                    }else{
                        multiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
                    }
                }
                if (activieListBaseResponse.data.getData().size() == 0) {
                    activeAdapter.loadMoreEnd();
                    return;
                }
                mActives.addAll(activieListBaseResponse.data.getData());
                activeAdapter.notifyDataSetChanged();
                activeAdapter.loadMoreComplete();
            }

            @Override
            public void onFailer(String msg) {
                mRefreshlayout.setRefreshing(false);
            }

            @Override
            public void onTokenError() {
                mRefreshlayout.setRefreshing(false);
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
